package com.example.caimogu.Goods2Json;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.zip.GZIPOutputStream;

/**
 * Created by Caimogu on 2016/4/21.
 */
public class Pic2String {

    private String filePath;
    private File file;
    public Pic2String(String filePath){
        this.filePath = filePath;
    }

    public String pic2Str(){
        byte[] data = loadFile();
        return new String(data,0,data.length);
    }

    public  String file2String(String encoding) {
        InputStreamReader reader = null;
        StringWriter writer = new StringWriter();
        try {
            if (encoding == null || "".equals(encoding.trim())) {
                reader = new InputStreamReader(new FileInputStream(file));
            } else {
                reader = new InputStreamReader(new FileInputStream(file),encoding);
            }
            //将输入流写入输出流
            char[] buffer = new char[1024];
            int n = 0;
            while (-1 != (n = reader.read(buffer))) {
                writer.write(buffer, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        //返回转换结果
        if (writer != null)
            return writer.toString();
        else return null;
    }

    public byte[] loadFile(){

        //file = new File("C:/Caimogu/AndroidStudioProjects/BusinessAPP/sheep.png");
        //file = new File("/mnt/sdcard/Pictures/sheep.png");
        file = new File(filePath);

        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;
        byte[] data = null ;

        try {
            fis = new FileInputStream(file);
            baos = new ByteArrayOutputStream((int) file.length());

            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }

            data = baos.toByteArray() ;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                    fis = null;
                }

                baos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

            return data;
    }
//    public byte[] compress(byte[] data){
//
//        Log.i("caimogu","before:" + new String(data,0,data.length));
//        GZIPOutputStream gzip = null ;
//        ByteArrayOutputStream baos = null ;
//        byte[] newData = null ;
//
//        try {
//            baos = new ByteArrayOutputStream();
//            gzip = new GZIPOutputStream(baos);
//
//            gzip.write(data);
//            gzip.finish();
//            gzip.flush();
//
//            newData = baos.toByteArray() ;
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                gzip.close();
//                baos.close() ;
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//        //System.out.println("after:" + newData.length);
//        Log.i("caimogu","after:" +  new String(newData,0,data.length));
//        return newData ;
//    }

}
